﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Server.Model;
using System.Data;

namespace Server.Data {
    public class CommandDAO:BaseDAO {
        public void SetCommand(Status pStatus, long pClientID, long pUserID, string pUserName)
        {
            base.ExecuteNonQuery("sp_Insert_Command_CommandQueue", pClientID, pStatus, pUserID, pUserName);
        }

        public string GetStatus(long pClientID, long pOperID, string pOperName)
        {
            string ret = string.Empty;
            DataTable dt = base.ExecuteDataTable("sp_Get_ClientStatus", pClientID);

            if (dt != null)
            {
                ret = dt.Rows[0]["Status"] as string;
            }

            return ret;
        }

        public List<StatusHistory> GetStatusHistory(long pClientID)
        {
            List<StatusHistory> ret = new List<StatusHistory>();
            DataTable dt = base.ExecuteDataTable("sp_Get_ClientStatus", pClientID);

            if (dt != null)
            {
                StatusHistory lastEnttiy = null;
                foreach (DataRow row in dt.Rows)
                {
                    StatusHistory history = new StatusHistory();
                    history.StartTime = DateTime.Parse(row["CreateOn"].ToString());
                    history.Status = MappingStatus(row["Status"] as string);

                    ret.Add(history);
                    if (lastEnttiy != null)
                    {
                        lastEnttiy.EndTime = history.StartTime;
                    }
                    lastEnttiy = history;
                }
            }

            return ret;
        }

        public void UpdateStatus(Status pStatus, long pClientID, long pOperID, string pOperName)
        {
            base.ExecuteNonQuery("sp_Insert_ClientStatus", pClientID, pStatus.ToString(), pOperID, pOperName);
        }

        public Status? UpdateStatusAndGetLatestedCommand(Status pStatus, long pClientID, long pOperID, string pOperName)
        {
            Status? ret = null;
            UpdateStatus(pStatus, pClientID, pOperID, pOperName);
            string retStr = base.ExecuteScalar("sp_Get_Command_CommandByClinetID", pClientID, pStatus.ToString(), pOperID, pOperName) as string;
            if (!string.IsNullOrEmpty(retStr))
            {
                ret = (Status)Enum.Parse(typeof(Status), retStr);
            }

            return ret;
        }

        private string MappingStatus(string status)
        {
            string ret = string.Empty;
            switch (status)
            {
                case "Lock":
                    ret = "锁屏";
                    break;
                case "Unlock":
                    ret = "使用中";
                    break;
                case "ServiceStop":
                    ret = "服务停止";
                    break;
                case "PlanPass":
                    ret = "计划通过";
                    break;
                case "PlanDeny":
                    ret = "计划拒绝";
                    break;
                case "Sleep":
                    ret = "关机";
                    break;
            }

            return ret;
        }
    }
}
